using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using eMisc;

namespace eLib.Dialogs
{
    public partial class frmXls : Form
    {
        public frmXls()
        {
            InitializeComponent();
        }

        public static bool GetTempateData(string DataName,
            ref string book, ref string sheet, string path0)
        {
            using (frmXls f = new frmXls())
            {
                Directory.SetCurrentDirectory(path0);
                f._path = path0;
                f.eData.Text = DataName;
                f.eFile.Text = book;
                if (book == "") f.fn = "";
                else f.fn = Path.GetFullPath(book);
                f.cbSheet.Text = sheet;
                if (book != "" && File.Exists(f.fn)) 
                    f.openFileDialog.InitialDirectory = new FileInfo(f.fn).Directory.ToString();
                else 
                    f.openFileDialog.InitialDirectory=path0;
                f.getSheets();
                bool res = f.ShowDialog() == DialogResult.OK;
                if (res)
                {
                    book = f.eFile.Text.Trim();
                    sheet = f.cbSheet.Text;
                }
                return res;
            }
        }

        string _path,fn;

        private void btnFile_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (openFileDialog.FileName.StartsWith(_path))
                    eFile.Text = openFileDialog.FileName.Substring(_path.Length + 1/*Path.PathSeparator*/);
                else
                    eFile.Text = openFileDialog.FileName;
                fn = openFileDialog.FileName;
                getSheets();
            }
        }


        private void getSheets()
        {
            cbSheet.Items.Clear();
            if (fn != "" && File.Exists(fn))
            {
                string err;
                List<string> L = new List<string>();
                WorkSheetUtils.GetSheetNames(fn, L, out err);
                cbSheet.Items.AddRange(L.ToArray());
            }
        }

        private void eFile_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Return)
                getSheets();
        }
    }
}